﻿using Microsoft.Extensions.AI;
using TorchSharp;
using static TorchSharp.torch;
using Qwen3;
using Qwen3.Module;
using System.Text.Json;
using System.Text;

public class Program
{
    public async static void TestQwen3()
    {
        Console.OutputEncoding = Encoding.UTF8;
        torch.InitializeDeviceType(DeviceType.CUDA);
        var defaultType = ScalarType.Float16;
        torch.set_default_dtype(defaultType);
        string device = "cuda";
        var weightFolder = @"G:\model\Qwen3-4B-Instruct-2507";

        var modelConfigPath = Path.Join(weightFolder, "config.json");
        var modelConfig = JsonSerializer.Deserialize<Qwen3Config>(File.ReadAllText(modelConfigPath));
        var model = new Qwen3MoE(modelConfig);

        model = model.half();
        model.load("G:\\MCP\\Qwen3\\Qwen3-4B-Instruct-2507-fp16.dat");
        model = model.to(device);

        var tokenizer = new Qwen3Tokenizer(Path.Combine(weightFolder, "tokenizer.json"));
        Console.WriteLine("load model success");
        var pipeline = new Qwen3MoEPipeline(model, tokenizer, device);
        var client = new Qwen3MoEChatClient(pipeline);

        //中国首都是北京吗?
        var task = """
            用c#写一个计算当前月天数的方法
            """;

        List<ChatMessage> _chatHistory = new();
        _chatHistory.Add(new ChatMessage(ChatRole.User, task));
        var options = new ChatOptions
        {
            MaxOutputTokens = 1024,
            Temperature = 0.7f
        };
        //var ret = await client.GetResponseAsync(_chatHistory, options);
        //Console.Write(ret.Text);
        await foreach (var response in client.GetStreamingResponseAsync(_chatHistory, options))
        {
            Console.Write(response.Text);
        }
    }
    
    public static void Main()
    {
        TestQwen3();
    }
}